package pers.yurwisher.rubick.manager.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import pers.yurwisher.rubick.common.annotation.rest.AnonymousPostMapping;
import pers.yurwisher.rubick.common.base.BaseController;
import pers.yurwisher.rubick.common.user.CustomAccessToken;
import pers.yurwisher.rubick.common.wrapper.R;
import pers.yurwisher.rubick.manager.pojo.auth.AccessTokenForm;
import pers.yurwisher.rubick.manager.pojo.auth.CurrentUserInfo;
import pers.yurwisher.rubick.manager.service.AuthorizationService;

import javax.servlet.http.HttpServletRequest;

/**
 * @author yq
 * @date 2020/09/18 09:22
 * @description 授权
 * @since V1.0.0
 */
@RestController
@RequestMapping("/oauth")
@RequiredArgsConstructor
public class AuthorizationController extends BaseController {

    private final AuthorizationService authorizationService;

    @AnonymousPostMapping(value = "/token")
    public R<CustomAccessToken> token(@Validated AccessTokenForm form, HttpServletRequest request) {
        return R.ok(authorizationService.token(form, request));
    }

    @GetMapping(value = "/info")
    public R<CurrentUserInfo> info() {
        return R.ok(authorizationService.currentUserInfo());
    }

    @PostMapping(value = "/logout")
    public R<String> logout(HttpServletRequest request) {
        authorizationService.logout(request);
        return R.ok();
    }

}
